package com.yh.workbill.ui.me.mvp;

import android.content.SharedPreferences;

import com.yh.workbill.App;
import com.yh.workbill.AppGlobals;
import com.yh.workbill.db.UserInfo;
import com.yh.workbill.db.UserInfoDao;
import com.yh.workbill.model.WorkBillCount;
import com.yh.workbill.network.BaseCallback;
import com.yh.workbill.network.OkHttpService;

import androidx.arch.core.executor.ArchTaskExecutor;

import static com.yh.workbill.utils.Config.SP_USER;

public class Me_Model {
    interface WorkBillCountCallback {
        void onSuccess(WorkBillCount data);
    }

    interface UserInfoCallBack {
        void onSuccess(String phone);
    }

    public void getUserInfo(UserInfoCallBack callBack) {
        ArchTaskExecutor.getIOThreadExecutor().execute(() -> {
            SharedPreferences sp = AppGlobals.getApplication().getSharedPreferences(SP_USER, AppGlobals.getApplication().MODE_PRIVATE);
            String phone = sp.getString("phone", "");
            ArchTaskExecutor.getMainThreadExecutor().execute(() -> callBack.onSuccess(phone));
        });
    }

    public void getWorkBillCount(WorkBillCountCallback callback) {
        ArchTaskExecutor.getIOThreadExecutor().execute(() -> {
            SharedPreferences sp = AppGlobals.getApplication().getSharedPreferences(SP_USER, AppGlobals.getApplication().MODE_PRIVATE);
            String phone = sp.getString("phone", "");
            UserInfoDao userInfoDao = App.appDataBase.getUserInfoDao();
            UserInfo singleUserInfo = userInfoDao.getSingleUserInfo(phone);
            String user_uuid = singleUserInfo.getUuid();
            OkHttpService.getInstance().getWorkBillCount(user_uuid, new BaseCallback<WorkBillCount>() {
                @Override
                public void accessSuccess(int code, String msg, WorkBillCount data) {
                    ArchTaskExecutor.getMainThreadExecutor().execute(() -> {
                        if (code == 200) {
                            callback.onSuccess(data);
                        }
                    });
                }

                @Override
                public void accessFailure(String errorMessage) {

                }
            });
        });
    }

}
